<template>
    <div class="login-body">
        <div class="login-container">
            <div class="head">
                <img class="logo" src="../assets/imgs/ypq.png" />
                <div class="name">
                    <div class="title">悦跑圈</div>
                </div>
            </div>

            <el-form class="login-form" ref="loginForm" :model="state.ruleForm" :rules="state.rules" label-width="80px">
        <el-form-item label="账号" prop="username">
          <el-input v-model="state.ruleForm.username" type="text"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="state.ruleForm.password" type="password"></el-input>
        </el-form-item>
      </el-form>

            <el-row type="flex" justify="center">
                <el-button type="primary"  @click="submitForm">登录</el-button>
                <el-button type="primary" @click="register">注册</el-button>
            </el-row>
        </div>
    </div>
</template>

<script setup>
  import axios from '@/utils/axios'
  import md5 from 'js-md5'
  import { reactive, ref } from 'vue'
  import { localSet } from '@/utils'
  import { ElMessage } from 'element-plus'
  import { useRouter } from 'vue-router'
  import { setUserInfo } from '@/utils/auth';
  const router = useRouter()
  const loginForm = ref(null)
  const state = reactive({
    ruleForm: {
      username: '',
      password: ''
    },
    checked: true,
    rules: {
      username: [
        { required: 'true', message: '账户不能为空', trigger: 'blur' }
      ],
      password: [
        { required: 'true', message: '密码不能为空', trigger: 'blur' }
      ]
    }
  })
  const submitForm = async () => {
loginForm.value.validate((valid) => {
  if (valid) {
    axios.post('/users/login', {
      username: state.ruleForm.username || '',
      password: md5(state.ruleForm.password)
    }).then(res => {
      console.log("res:",res)
      if(res.code==200){
        // localSet('token', res.data)
        localSet('token', res.data.token);
        setUserInfo(res.data.userInfo); // 存储用户信息
        // window.location.href = '/home'
        router.push({ path: '/home' })
      }else{
        ElMessage({
          message:"登录失败",
          duration:2000,
          type:'error'
        })
      }
    })
  } else {
    console.log('error submit!!')
    return false;
  }
})
  }
const resetForm = () => {
    loginForm.value.resetFields();
}
const register=()=>{
    router.push({ path: '/register' })
}
</script>

<style scoped>
.login-body {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100vh;
    background-color: #fff;
}

.login-container {
    width: 420px;
    height: 500px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0px 21px 41px 0px rgba(0, 0, 0, 0.2);
}

.head {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 40px 0 20px 0;
}

.head img {
    width: 100px;
    height: 100px;
    margin-right: 20px;
}

.head .title {
    font-size: 28px;
    color: #1BAEAE;
    font-weight: bold;
}

.head .tips {
    font-size: 12px;
    color: #999;
}

.login-form {
    width: 70%;
    margin: 0 auto;
    margin-bottom: 20px; /* 在输入框和按钮之间增加 20px 的间距 */
}

.login-form .el-form-item {
    margin-bottom: 20px; /* 在每个表单项之间增加 20px 的间距 */
}

.login-form>>>.el-form--label-top .el-form-item__label {
    padding: 0;
}

.el-row {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
}
</style>